package com.cetcs.kmga.common;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileUtil {
    private final static String fileName = "D:\\index.txt";

    public static boolean createIndex(){
        boolean ok = false;
        try{
            File file = new File(fileName);
            if(file.exists()){
                file.delete();
                return file.createNewFile();
            }
            return file.createNewFile();
        }
        catch (Exception ex)
        {
            return ok;
        }
    }

    public static void writeIndex(String data){
        try {
            appendUsingBufferedWrite(fileName, data, 1);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    private static void appendUsingBufferedWrite(String fileName, String data, int noOfLines) throws IOException {
        File file = new File(fileName);
        FileWriter fw = null;
        BufferedWriter bw = null;
        //true:表示是追加的标志
        fw = new FileWriter(file, true);
        bw = new BufferedWriter(fw);
        //开始输出写入文件
        for (int i = 0; i < noOfLines; i++) {
            bw.newLine();
            fw.write(data);
        }
        bw.close();
        fw.close();
    }
}